package com.gwz.bus.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwz.bus.constast.BUSConstast;
import com.gwz.bus.domain.Provider;
import com.gwz.bus.service.ProviderService;
import com.gwz.bus.vo.ProviderVo;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.utils.ResultObj;
import com.gwz.sys.utils.TreeNode;

@RestController
@RequestMapping("provider")
public class ProviderController {
	
	@Autowired
	private ProviderService providerService;
	
	@RequestMapping("loadAllProvider")
	public DataGridView loadAllProvider(ProviderVo  providerVo) {
		providerVo.setAvailable(BUSConstast.BUS_AVAILABLE_TRUE);
		return providerService.queryAllProvider(providerVo);
	}
	
	@RequestMapping("addProvider")
	public ResultObj addProvider(ProviderVo providerVo) {
		ResultObj resultObj = new ResultObj(200, "添加成功");
		try {
			providerService.addProvider(providerVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("添加失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("updateProvider")
	public ResultObj updateProvider(ProviderVo providerVo) {
		ResultObj resultObj = new ResultObj(200, "修改成功");
		try {
			providerService.updateProvider(providerVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("修改失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("deleteProvider")
	public ResultObj deleteProvider(ProviderVo providerVo) {
		ResultObj resultObj = new ResultObj(200, "删除成功");
		try {
			providerService.deleteProvider(providerVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("删除失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("batchDelete")
	public ResultObj batchDelete(ProviderVo providerVo) {
		ResultObj resultObj = new ResultObj(200, "删除成功");
		try {
			providerService.batchDelete(providerVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("删除失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("loadGoodsLeftJson")
	public List<TreeNode> loadGoodsLeftJson(){
		ProviderVo providerVo = new ProviderVo();
		providerVo.setAvailable(BUSConstast.BUS_AVAILABLE_TRUE);
		List<Provider> list = providerService.queryAllProviderForList(providerVo);
		List<TreeNode> treeNodes = new ArrayList<>();
		treeNodes.add(new TreeNode(0, 0, "所有供应商", true, true));
		for (Provider p : list) {
			treeNodes.add(new TreeNode(p.getId(), 0, p.getProvidername(), false, false));
		}
		return treeNodes;
	}
	
	@RequestMapping("loadProviderSelect")
	public List<Provider> loadProviderSelect(){
		ProviderVo providerVo = new ProviderVo();
		providerVo.setAvailable(BUSConstast.BUS_AVAILABLE_TRUE);
		return providerService.queryAllProviderForList(providerVo);
	}
	
}
